<?php
namespace Admin\Controller;
use Think\Controller;
class YygameController extends GloableController {



      //添加-更新数据特殊用户
      public function update_game_user()
      {
        $prizes_list=M("yyprizes")->where("is_lock=0 and level in('1','2')")->order("level asc")->select();

        if($_POST['code'])
        {
           $code=I('code','');
           $prizes_id=I('prizes_id');
           $yyusertoprize_info=M("yyusertoprize")->where("code='$code' and status!=2")->find();
           if($yyusertoprize_info)
           {
             echo "参与码已指定用户";
              exit();
           }

          //检查是否已使用code
           $prizes_info=M("yycode")->where("code='$code'")->find();
           if(!$prizes_info)
           {
              echo "参与码不存在";
              exit();

           }
           else if($prizes_info['status']==1)
           {
               echo "参与码已使用，使用时间：".date("Y-m-d H:i:s",$prizes_info['used_time']);
               exit();
           }
           else
           {
            
            $data['prizes_id']=$prizes_id;

            $data['code']=$code;
            $data['u_order']=I('order',0,'int');
            $data['ctime']=time();

            $status_dec=M("yyprizes")->where("prizes_id='$prizes_id' and surplus>0")->setDec("surplus",1);
            if($status_dec)
            {

             $status=M("yyusertoprize")->add($data);
             if($status)$mes=$code."指定奖励成功";else $mes=$code."指定失败";
            }
           else
           {$mes=$code."指定失败,奖池内已不存在该奖项，请等待下轮";}
             

             echo $mes;
             exit();     
           }



        }

        


        $this->assign("prizes_list",$prizes_list);
        

        
        $this->display();
        
      }
       //取消指定参与码
      public function del_code_used()
      {
          $u_prize_id=I('u_prize_id');
          //检查是否已使用code
           $yyusertoprize_info=M("yyusertoprize")->where("u_prize_id='$u_prize_id'")->find();
           $prizes_id=$yyusertoprize_info['prizes_id'];
           if($yyusertoprize_info['status']!=0)
           {
              echo "参与码已取消/已使用";
              exit();
           }
           else
           {

              $update_data['used_time']=time();
              $update_data['status']=2;
              $status=M("yyusertoprize")->where("u_prize_id='$u_prize_id'")->save($update_data);
              if($status)
              {

                $mes=$code."取消指定成功";
                $status_dec=M("yyprizes")->where("prizes_id='$prizes_id'")->setInc("surplus",1);
              }

              else $mes=$code."取消指定失败";

               echo $mes;
               exit(); 
           }

           
          
      }

      //特殊用户得奖列表
      public function user_prizes_list()
      {
         $code=I('code');
         $page=I('p');
         if($code)
         {
          $where['code']=$code;
          $this->assign('code',$code);
         }
         $list=D("Yyusertoprize")->get_list($page,10,$where);

         $this->assign('info1',$list);

           
        $this->display();
        
      }
      //使用参与码
      public function code_used()
      {
          $code_id=I('code_id');
          //检查是否已使用code
           $prizes_info=M("yycode")->where("code_id='$code_id'")->find();
           $code=$prizes_info['code'];
           if(!$prizes_info)
           {
              $mes= "使用失败，参与码不存在";
              

           }
           else if($prizes_info['status']==1)
           {
               $mes= "使用失败，参与码已使用，使用时间：".date("Y-m-d H:i:s",$prizes_info['used_time']);
              
           }
           else
           {
              $up['status']=1;
              $up['used_time']=time();
              $up['remark']='后台直接使用';
              $status=M("yycode")->where("code_id='$code_id'")->save($up);
              if($status)$mes=$prizes_info['code']."使用成功";else $mes=$prizes_info['code']."使用失败";

              $u_update['status']=1;
              $u_update['used_time']=time();
              $st=M("yyusertoprize")->where("code='$code'")->save($u_update);

  

           }

           echo $mes;exit;

      }
      //编辑参与列表
      public function code_list()
      {
         $code=I('code');
         $page=I('p');
         if($code)
         {
          $where['code']=$code;
          $this->assign('code',$code);
         }
         $list=D("Yyusertoprize")->get_code_list($page,10,$where);

         $this->assign('info1',$list);

         $list_prizes=M("yyprizes")->order('level asc')->select();
         $this->assign('list_prizes',$list_prizes);

         $last_ct=M("yyprizes_log")->count();
         $this->assign('last_ct',$last_ct);

           
         $this->display();

      }




    


    

  

}
